package com.xyd.servlet;

import com.xyd.bean.Admin;
import com.xyd.service.AdminService;
import com.xyd.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet({"/manage/adminLogin"})
public class AdminServlet extends HttpServlet {
    AdminService adminService = new AdminServiceImpl();
   /* @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String path = request.getServletPath();
        if ("/adminLogin".equals(path)){
            doLogin(request,response);
        }
    }*/

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");

        Admin admin = adminService.login(new Admin(userName, password));
        if (admin != null){
            HttpSession session = request.getSession();
            session.setAttribute("admin",admin);
            request.getRequestDispatcher("/manage/index.jsp").forward(request,response);
        }
    }

   /* private void doLogin(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }*/
}
